package com.moss.system.service.impl;


import com.moss.common.entity.SysRoleMenuEntity;
import com.moss.common.entity.SysUserEntity;
import com.moss.common.exception.ServiceException;
import com.moss.common.entity.SysRoleEntity;
import com.moss.system.mapper.RoleMapper;
import com.moss.system.mapper.RoleMenuMapper;
import com.moss.system.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author gxcc3
 * @date: 2024/8/12 22:27
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    RoleMapper roleMapper;

    @Resource
    RoleMenuMapper roleMenuMapper;


    @Override
    public List<SysRoleEntity> getRoleList(SysRoleEntity role) {
        return roleMapper.getRoleList(role);
    }

    @Override
    public Set<String> selectRolePermissionByUserId(String userId) {
        List<SysRoleEntity> perms = roleMapper.selectRolePermissionByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (SysRoleEntity perm : perms) {
            if (!Objects.equals(null,perm)) {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRole(SysRoleEntity role) {
        checkParam(role,true);
        roleMapper.insert(role);
        insertRoleMenuBatch(role);
    }

    private int insertRoleMenuBatch(SysRoleEntity role) {
        int row = 1;
        Long[] menuIds = role.getMenuIds();
        Long roleId = role.getRoleId();
        List<SysRoleMenuEntity> list = new ArrayList<>();
        for (Long menuId : menuIds) {
            SysRoleMenuEntity roleMenu = new SysRoleMenuEntity();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            list.add(roleMenu);
        }
        if (!list.isEmpty()){
            row = roleMenuMapper.insertBatch(list);
        }
        return row;
    }


    private void checkParam(SysRoleEntity role, boolean save) {
        //遍历提交数据
        if (role == null){
            throw new ServiceException("提交数据为空");
        }
        if (StringUtils.isEmpty(role.getRoleName())){
            throw new ServiceException("角色名为空");
        }
        if (StringUtils.isEmpty(role.getRoleKey())){
            throw new ServiceException("角色编码为空");
        }
        if (role.getSort() == null){
            throw new ServiceException("请输入排序号");
        }
        if (role.getStatus() == null){
            role.setStatus(1);
        }
        if (save){
            //新增
            SysRoleEntity byRoleName = roleMapper.selectRoleByRoleName(role.getRoleName());
            if (byRoleName != null){
                throw new ServiceException("角色已存在");
            }
            SysRoleEntity byRoleKey = roleMapper.selectRoleByRoleKey(role.getRoleKey());
            if (byRoleKey != null){
                throw new ServiceException("角色编码已存在");
            }
        }else {
            //修改
            SysRoleEntity byRoleName = roleMapper.selectRoleByRoleName(role.getRoleName());
            if (byRoleName != null && !Objects.equals(role.getRoleId(), byRoleName.getRoleId())){
                throw new ServiceException("角色已存在");
            }
            SysRoleEntity byRoleKey = roleMapper.selectRoleByRoleKey(role.getRoleKey());
            if (byRoleKey != null && !Objects.equals(role.getRoleId(), byRoleKey.getRoleId())){
                throw new ServiceException("角色编码已存在");
            }
        }
    }


    @Override
    public Integer findMaxSort() {
        Integer sort = roleMapper.findMaxSort();
        return sort == null ? 0 : sort + 1;
    }


    @Override
    public SysRoleEntity getById(Long roleId) {
        return roleMapper.selectById(roleId);
    }

    @Override
    public void updateRole(SysRoleEntity role) {
        checkParam(role,false);
        roleMapper.updateRole(role);
        roleMenuMapper.deleteRoleMenuByRoleId(role.getRoleId());
        insertRoleMenuBatch(role);
    }

    @Override
    public void updateRoleStatus(SysRoleEntity role) {
        roleMapper.updateRole(role);
    }

    @Override
    public void deleteRole(Long[] roleIds) {
        roleMapper.removeBatchIds(roleIds);
    }


    @Override
    public List<SysRoleEntity> selectRoleAll() {
        return roleMapper.getRoleList(new SysRoleEntity());
    }

    @Override
    public Long[] selectUserRoleIds(String userId) {
        return roleMapper.selectUserRoleIds(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserRole(SysUserEntity user) {
        String userId = user.getUserId();
        Long[] roleIds = user.getRoleIds();
        //清空用户之前拥有的角色
        roleMapper.removeUserRole(userId);
        roleMapper.insertUserRole(userId,roleIds);
    }
}
